package com.sjc.designpattern.decorator;


/**
 * @Classname DecoratorPatternDemo
 * @Description 装饰器模式：对目标类的功能进行增强
 * 1. java的io体系，一层包装一层，外面的一层，都会对里面的一层进行功能的增强。
 * 2. spring的aop，基于动态代理的理念，装饰目标对象，加入事务控制、日志打印之类的功能
 * @Date 2020/4/26 13:37
 * @Created by SangJiacun
 */
public class DecoratorPatternDemo {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decorator = new Decorator(component);
        decorator.execute();
    }

    public interface Component{
        void execute();
    }

    public static class ConcreteComponent implements Component{
        @Override
        public void execute() {
            System.out.println("执行基础功能！");
        }
    }

    public static class Decorator implements  Component{

        private Component component;

        public Decorator(Component component) {
            this.component = component;
        }

        @Override
        public void execute() {
            System.out.println("在执行基础功能之前，执行部分功能增强");
            component.execute();
            System.out.println("在执行基础功能之后，执行部分功能增强");
        }
    }
}
